package com.bjpowernode.boss.mvc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.boss.PowerBossApplication;
import com.bjpowernode.boss.constant.SystemConstant;
import com.bjpowernode.boss.enums.RespInfo;
import com.bjpowernode.boss.exception.PowerException;
import com.bjpowernode.boss.mvc.models.dic.DicItem;
import com.bjpowernode.boss.mvc.models.query.PayProductConfigQuery;
import com.bjpowernode.boss.mvc.service.PaymentService;
import com.bjpowernode.boss.persist.domain.PayProduct;
import com.bjpowernode.boss.persist.domain.PayWay;
import com.bjpowernode.boss.persist.mapper.PayProductMapper;
import com.bjpowernode.boss.persist.mapper.PayWayMapper;
import com.bjpowernode.boss.utils.IdUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Resource
    private PayProductMapper payProductMapper;

    @Resource
    private PayWayMapper payWayMapper;

    @Override
    public Page<PayProduct> loadPage(Integer pageNo) {

        return payProductMapper.selectPage(Page.of(pageNo, SystemConstant.PAGE_SIZE), new QueryWrapper<>());

    }

    @Override
    public List<DicItem> getAllProduct() {
        return payProductMapper.selectAll();
    }

    @Override
    public List<DicItem> getAllWay() {
        return payWayMapper.selectAll();
    }

    @Override
    public List<DicItem> getPayTypeList(String payWayCode) {
        if (PowerBossApplication.PAY_TYPE_GROUP_DIC.containsKey(payWayCode)) {
            return PowerBossApplication.PAY_TYPE_GROUP_DIC.get(payWayCode);
        }
        return new ArrayList<>();
    }

    @Override
    public void addPayWay(PayProductConfigQuery payProductConfigQuery) {

        //判断支付方式是否存在
        Long count = payWayMapper.selectCount(new QueryWrapper<PayWay>()
                                              .eq("way_code", payProductConfigQuery.getPayWayCode())
                                              .eq("type_code", payProductConfigQuery.getPayTypeCode())
                                              .eq("pay_product_code", payProductConfigQuery.getPayProductCode())
                                             );
        if (count != 0) {
            throw new PowerException(RespInfo.PAY_WAY_EXIST_ERROR);
        }

        //不存在则添加
        PayWay payWay = new PayWay();
        payWay.setId(IdUtil.uuid());
        payWay.setWayCode(payProductConfigQuery.getPayWayCode());
        payWay.setWayName(PowerBossApplication.PAY_WAY_DIC.get(payProductConfigQuery.getPayWayCode()));
        payWay.setTypeCode(payProductConfigQuery.getPayTypeCode());
        payWay.setTypeName(PowerBossApplication.PAY_TYPE_DIC
                .get(payProductConfigQuery.getPayWayCode() + ":" + payProductConfigQuery.getPayTypeCode()));
        payWay.setPayProductCode(payProductConfigQuery.getPayProductCode());
        payWay.setPayRate(payProductConfigQuery.getPayRate());
        payWay.setSorts(payProductConfigQuery.getSorts());
        payWay.setStatus("ENABLE");
        payWay.setEditTime(new Date());
        payWay.setRemark("配置产品支付方式");
        payWayMapper.insert(payWay);

    }

    @Override
    public Page<PayWay> loadPayWayList(Integer pageNo, String productCode) {

        if (pageNo == null || pageNo <= 0) {
            pageNo = 1;
        }

        QueryWrapper<PayWay> queryWrapper = new QueryWrapper<>();

        if (StrUtil.isNotBlank(productCode)) {
            queryWrapper.eq("pay_product_code", productCode);
        }

        queryWrapper.orderByAsc("pay_product_code");
        return payWayMapper.selectPage(Page.of(pageNo, SystemConstant.PAGE_SIZE), queryWrapper);
    }

    @Override
    public void removePayWay(String id) {
        int i = payWayMapper.deleteById(id);
        if (i <= 0) {
            throw new PowerException(RespInfo.PAY_WAY_NOT_EXIST_ERROR);
        }
    }

    @Override
    public List<PayProduct> matchProductName(String productName) {
        return payProductMapper.selectList(new QueryWrapper<PayProduct>().like("product_name", productName));
    }
}







